home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / CMDENABL.PAK / CMDENAB4.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  6KB  |  290 lines

  1. // ---------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (C) 1994, 1995 by Borland International, All Rights Reserved
  4. //
  5. //  Command-enabling sample application
  6. //  Fourth version adds a tool bar.
  7. //----------------------------------------------------------------------------
  8. #include <owl/pch.h>
  9. #include <owl/applicat.h>
  10. #include <owl/framewin.h>
  11. #include <owl/decframe.h>
  12. #include <owl/controlb.h>
  13. #include <owl/buttonga.h>
  14. #include <owl/statusba.h>
  15. #include "cmdenabl.h"
  16.  
  17. //
  18. // class TCmdEnableApp
  19. // ~~~~~ ~~~~~~~~~~~~~
  20. // Application object initializes TApplication, overrides InitMainWindow.
  21. //
  22. class TCmdEnableApp : public TApplication {
  23.   public:
  24.     TCmdEnableApp() : TApplication() {}
  25.  
  26.   protected:
  27.     void InitMainWindow();
  28. };
  29.  
  30.  
  31. //
  32. // class TCmdEnableWindow
  33. // ~~~~~ ~~~~~~~~~~~~~~~~
  34. // Window object initializes TWindow, adds four event handlers,
  35. // adds response table for four events.
  36. class TCmdEnableWindow : public TWindow {
  37.   public:
  38.     TCmdEnableWindow(TWindow* parent = 0);
  39.  
  40.   protected:
  41.     // Event handlers
  42.     //
  43.     void CmFileNew();
  44.     void CmFileOpen();
  45.     void CmFileSave();
  46.     void CmFileSaveAs();
  47.     void CmToggleDirty();
  48.     void CmToggleNew();
  49.     void CmShowState();
  50.  
  51.     // Command-enabling functions
  52.     //
  53.     void CeFileNew(TCommandEnabler&);
  54.     void CeFileOpen(TCommandEnabler&);
  55.     void CeFileSave(TCommandEnabler&);
  56.     void CeToggleDirty(TCommandEnabler&);
  57.     void CeToggleNew(TCommandEnabler&);
  58.  
  59.     bool IsDirty;
  60.     bool IsNewFile;
  61.  
  62.   DECLARE_RESPONSE_TABLE(TCmdEnableWindow);
  63. };
  64.  
  65.  
  66. //
  67. //
  68. //
  69. DEFINE_RESPONSE_TABLE1(TCmdEnableWindow, TWindow)
  70.   EV_COMMAND(CM_FILENEW, CmFileNew),
  71.   EV_COMMAND(CM_FILEOPEN, CmFileOpen),
  72.   EV_COMMAND(CM_FILESAVE, CmFileSave),
  73.   EV_COMMAND(CM_FILESAVEAS, CmFileSaveAs),
  74.   EV_COMMAND(CM_TOGGLEDIRTY, CmToggleDirty),
  75.   EV_COMMAND(CM_TOGGLENEW, CmToggleNew),
  76.   EV_COMMAND(CM_SHOWSTATE, CmShowState),
  77.   EV_COMMAND_ENABLE(CM_FILENEW, CeFileNew),
  78.   EV_COMMAND_ENABLE(CM_FILEOPEN, CeFileOpen),
  79.   EV_COMMAND_ENABLE(CM_FILESAVE, CeFileSave),
  80.   EV_COMMAND_ENABLE(CM_TOGGLEDIRTY, CeToggleDirty),
  81.   EV_COMMAND_ENABLE(CM_TOGGLENEW, CeToggleNew),
  82. END_RESPONSE_TABLE;
  83.  
  84.  
  85. //
  86. // TCmdEnableApp::InitMainWindow
  87. //
  88. void
  89. TCmdEnableApp::InitMainWindow()
  90. {
  91.   // Construct the decorated frame window
  92.   //
  93.   TDecoratedFrame* frame = new TDecoratedFrame(0, "Command-enabling sample application",
  94.     new TCmdEnableWindow, true);
  95.  
  96.   //  Construct a control bar
  97.   //
  98.   TControlBar* cb = new TControlBar(frame);
  99.   cb->Insert(*new TButtonGadget(CM_FILENEW, CM_FILENEW, TButtonGadget::Command));
  100.   cb->Insert(*new TButtonGadget(CM_FILEOPEN, CM_FILEOPEN, TButtonGadget::Command));
  101.   cb->Insert(*new TButtonGadget(CM_FILESAVE, CM_FILESAVE, TButtonGadget::Command));
  102.   cb->Insert(*new TButtonGadget(CM_FILESAVEAS, CM_FILESAVEAS, TButtonGadget::Command));
  103.   cb->Insert(*new TSeparatorGadget);
  104.   cb->Insert(*new TButtonGadget(CM_TOGGLEDIRTY, CM_TOGGLEDIRTY, TButtonGadget::NonExclusive));
  105.   cb->Insert(*new TButtonGadget(CM_TOGGLENEW, CM_TOGGLENEW, TButtonGadget::NonExclusive));
  106.   cb->Insert(*new TButtonGadget(CM_SHOWSTATE, CM_SHOWSTATE, TButtonGadget::Command));
  107.  
  108.   // Turn hints on to fly-over
  109.   //
  110.   cb->SetHintMode(TGadgetWindow::EnterHints);
  111.  
  112.   // Create a status bar
  113.   //
  114.   TStatusBar* sb = new TStatusBar(frame);
  115.  
  116.   // Insert the control bar and status bar into the frame
  117.   //
  118.   frame->Insert(*cb, TDecoratedFrame::Top);
  119.   frame->Insert(*sb, TDecoratedFrame::Bottom);
  120.  
  121.   // Set the main window and its menu
  122.   //
  123.   SetMainWindow(frame);
  124.   GetMainWindow()->AssignMenu(MN_COMMANDS);
  125. }
  126.  
  127.  
  128. //
  129. //
  130. //
  131. TCmdEnableWindow::TCmdEnableWindow(TWindow* parent)
  132. :
  133.   TWindow(parent),
  134.   IsNewFile(true),
  135.   IsDirty(false)
  136. {
  137. }
  138.  
  139.  
  140. //
  141. //
  142. //
  143. void
  144. TCmdEnableWindow::CmFileNew()
  145. {
  146.   IsDirty   = false;
  147.   IsNewFile = true;
  148.   MessageBox("Opened a new file.\nIsDirty=false\nIsNewFile=true",
  149.     "File action taken");
  150. }
  151.  
  152.  
  153. //
  154. //
  155. //
  156. void
  157. TCmdEnableWindow::CmFileOpen()
  158. {
  159.   IsDirty   = false;
  160.   IsNewFile = false;
  161.   MessageBox("Opened an existing file.\nIsDirty=false\nIsNewFile=false",
  162.     "File action taken");
  163. }
  164.  
  165.  
  166. //
  167. //
  168. //
  169. void
  170. TCmdEnableWindow::CmFileSave()
  171. {
  172.   IsDirty   = false;
  173.   IsNewFile = false;
  174.   MessageBox("Saved an existing file.\nIsDirty=false\nIsNewFile=false",
  175.     "File action taken");
  176. }
  177.  
  178.  
  179. //
  180. //
  181. //
  182. void
  183. TCmdEnableWindow::CmFileSaveAs()
  184. {
  185.   IsDirty   = false;
  186.   IsNewFile = false;
  187.   MessageBox("Saved a file under a new name.\nIsDirty=false\nIsNewFile=false",
  188.     "File action taken");
  189. }
  190.  
  191.  
  192. //
  193. //
  194. //
  195. void
  196. TCmdEnableWindow::CmToggleDirty()
  197. {
  198.   IsDirty=!IsDirty;
  199. }
  200.  
  201.  
  202. //
  203. //
  204. //
  205. void
  206. TCmdEnableWindow::CmToggleNew()
  207. {
  208.   IsNewFile=!IsNewFile;
  209. }
  210.  
  211.  
  212. //
  213. //
  214. //
  215.  
  216. void
  217. TCmdEnableWindow::CmShowState()
  218. {
  219.   string str(IsDirty ? "IsDirty = true\n" : "IsDirty = false\n");
  220.   str += (IsNewFile ? "IsNewFile = true" : "IsNewFile = false");
  221.   MessageBox(str.c_str(), "Current state");
  222. }
  223.  
  224.  
  225. //
  226. //
  227. //
  228. void
  229. TCmdEnableWindow::CeFileNew(TCommandEnabler& ce)
  230. {
  231.   // Enable CmFileNew if not dirty
  232.   //
  233.   ce.Enable(!IsDirty);
  234. }
  235.  
  236.  
  237. //
  238. //
  239. //
  240. void
  241. TCmdEnableWindow::CeFileOpen(TCommandEnabler& ce)
  242. {
  243.   // Enable CmFileOpen if not dirty
  244.   //
  245.   ce.Enable(!IsDirty);
  246. }
  247.  
  248.  
  249. //
  250. //
  251. //
  252. void
  253. TCmdEnableWindow::CeFileSave(TCommandEnabler& ce)
  254. {
  255.   // Enable CmFileSave if not new file and is dirty.
  256.   //
  257.   ce.Enable(!IsNewFile && IsDirty);
  258. }
  259.  
  260.  
  261. //
  262. //
  263. //
  264. void
  265. TCmdEnableWindow::CeToggleDirty(TCommandEnabler& ce)
  266. {
  267.   ce.SetCheck(IsDirty ? TCommandEnabler::Checked : TCommandEnabler::Unchecked);
  268. }
  269.  
  270.  
  271. //
  272. //
  273. //
  274. void
  275. TCmdEnableWindow::CeToggleNew(TCommandEnabler& ce)
  276. {
  277.   ce.SetCheck(IsNewFile ? TCommandEnabler::Checked : TCommandEnabler::Unchecked);
  278. }
  279.  
  280.  
  281. //
  282. // Put the OwlMain here just to get it out of the way
  283. //
  284. int
  285. OwlMain(int /*argc*/, char* /*argv*/ [])
  286. {
  287.   return TCmdEnableApp().Run();
  288. }
  289.  
  290.